package com.sds.controller;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.sds.dao.MemberDAO;
import com.sds.frontcontroller.FrontController_Interface;
import com.sds.util.Util_Servlet;
import com.sds.vo.MemberVO;

public class MemberController implements FrontController_Interface {
	private MemberDAO memberDAO = new MemberDAO();

	@Override
	public void execute(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		System.out.println("[ Location : MemberController ]");

		if("insertForm".equals(request.getParameter("type"))) {

		} else if("updateForm".equals(request.getParameter("type"))) {
			MemberVO memberVO = memberDAO.selectMember(request.getParameter("mem_id"));

			if(memberVO != null) {
				String mem_tel_1 = memberVO.getMem_tel().split("-")[0];
				String mem_tel_2 = memberVO.getMem_tel().split("-")[1];
				String mem_tel_3 = memberVO.getMem_tel().split("-")[2];
				
				request.setAttribute("MEMBER", memberVO);
				request.setAttribute("MEM_TEL_1", mem_tel_1);
				request.setAttribute("MEM_TEL_2", mem_tel_2);
				request.setAttribute("MEM_TEL_3", mem_tel_3);
				
				Util_Servlet.forward(request, response, "jsp_member/updateMemberForm.jsp");
			}
		} else if("insert".equals(request.getParameter("type"))) {
			String mem_id = request.getParameter("MEM_ID");
			String mem_pw = request.getParameter("MEM_PW");
			String mem_name = request.getParameter("MEM_NAME");
			String mem_tel = 
					request.getParameter("MEM_TEL_1") + "-" +
					request.getParameter("MEM_TEL_2") + "-" +
					request.getParameter("MEM_TEL_3");
			
			MemberVO memberVO = memberDAO.selectMember(mem_id);
			
			if(memberVO != null) {
				Util_Servlet.sendRedirect(response, "jsp_member/insertMemberForm.jsp");
			} else {
				if(mem_id != null && mem_pw != null && mem_name != null && mem_tel != null) {
					if(mem_id.length() != 0 && mem_pw.length() != 0 && mem_name.length() != 0 && mem_tel.length() !=0) {
						memberDAO.insertMember(new MemberVO(mem_id, mem_pw, mem_name, mem_tel, "0"));
						
						Util_Servlet.sendRedirect(response, "jsp_main/mainContent.jsp");
					} else {
						Util_Servlet.sendRedirect(response, "jsp_member/insertMemberForm.jsp");
					}
				} else {
					Util_Servlet.sendRedirect(response, "jsp_member/insertMemberForm.jsp");
				}
			}			
		} else if("select".equals(request.getParameter("type"))) {
			MemberVO memberVO = memberDAO.selectMember(request.getParameter("mem_id"));

			if(memberVO != null) {
				request.setAttribute("MEMBER", memberVO);

				Util_Servlet.forward(request, response, "jsp_member/selectMemberForm.jsp");
			}
		} else if("selectList".equals(request.getParameter("type"))) {		
			ArrayList<MemberVO> memberVOList = memberDAO.selectMemberList();

			request.setAttribute("MEMBER_LIST", memberVOList);

			Util_Servlet.forward(request, response, "jsp_member/selectMemberListForm.jsp");
		} else if("update".equals(request.getParameter("type"))) {
			String mem_id = request.getParameter("MEM_ID");
			String mem_pw = request.getParameter("MEM_PW");
			String mem_name = request.getParameter("MEM_NAME");
			String mem_tel = 
					request.getParameter("MEM_TEL_1") + "-" +
					request.getParameter("MEM_TEL_2") + "-" +
					request.getParameter("MEM_TEL_3");
			
			MemberVO memberVO = memberDAO.selectMember(mem_id);
			
			if(memberVO != null) {
				if(mem_id != null && mem_pw != null && mem_name != null && mem_tel != null) {
					if(mem_id.length() != 0 && mem_pw.length() != 0 && mem_name.length() != 0 && mem_tel.length() !=0) {
						memberDAO.updateMember(new MemberVO(mem_id, mem_pw, mem_name, mem_tel, "0"));
						
						HttpSession session = request.getSession(true);
						session.setAttribute("MEMBER", memberDAO.selectMember(mem_id));
						
						Util_Servlet.sendRedirect(response, "jsp_main/mainContent.jsp");
					} else {
						Util_Servlet.sendRedirect(response, "jsp_member/updateMemberForm.jsp");
					}
				} else {
					Util_Servlet.sendRedirect(response, "jsp_member/updateMemberForm.jsp");
				}
			} else {
				Util_Servlet.sendRedirect(response, "jsp_member/updateMemberForm.jsp");
			}			
		} else if("delete".equals(request.getParameter("type"))) {
			memberDAO.deleteMember(request.getParameter("mem_id"));
			
			ArrayList<MemberVO> memberVOList = memberDAO.selectMemberList();

			request.setAttribute("MEMBER_LIST", memberVOList);

			Util_Servlet.forward(request, response, "jsp_member/selectMemberListForm.jsp");
		}
	}
}